/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.webservicecomponent.metadata.parameter.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.webservicecomponent.metadata.parameter.HelpFetchValueSetting;
import com.inspur.edp.webservicecomponent.metadata.parameter.IParameterFetchValueSetting;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class ParameterFetchValueSettingSerializer {

    public static JsonNode serialize(IParameterFetchValueSetting fetchValueSetting) {
        if (fetchValueSetting instanceof HelpFetchValueSetting) {
            return HelpFetchValueSettingSerializer.serialize((HelpFetchValueSetting) fetchValueSetting);
        } else {
            throw new RuntimeException("Unsupported IParameterFetchValueSetting:" + fetchValueSetting.getKind());
        }
    }

    public static IParameterFetchValueSetting deserialize(JsonNode fetchValueSettingNode) {
        String kind = JsonUtil.getPropValue(fetchValueSettingNode, "kind", String.class);
        if (kind == null) {
            throw new RuntimeException("反序列化出错：缺少kind字段，无法匹配序列化器。JsonNode:" + fetchValueSettingNode.toString());
        }
        switch (kind) {
            case HelpFetchValueSetting.KIND:
                return HelpFetchValueSettingSerializer.deserialize(fetchValueSettingNode);
            default:
                throw new RuntimeException("Unsupported IParameterFetchValueSetting:" + kind);
        }
    }
}
